package com.emptyMe.org.service.impl;

import com.emptyMe.basic.utils.*;
import com.emptyMe.org.domain.Emp;
import com.emptyMe.org.domain.EmpDTO;
import com.emptyMe.org.mapper.EmpMapper;
import com.emptyMe.org.query.EmpQuery;
import com.emptyMe.org.service.IEmpService;
import com.emptyMe.user.domain.User;
import com.emptyMe.user.domain.UserDTO;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.*;
import java.util.concurrent.TimeUnit;

@Service
@Transactional(readOnly = true, propagation = Propagation.SUPPORTS)
public class EmpServiceImpl implements IEmpService {
    @Resource
    private EmpMapper mapper;
    @Autowired
    private RedisTemplate redisTemplate;


    @Override
    public EmpDTO login(Emp employee) {
        Emp emp = mapper.selectByUsername(employee.getUsername());
        if (emp == null) {
            // 用户名不存在
            throw new RuntimeException("用户名或密码错误");
        } else {
            // 比较密码
            if (Objects.equals(MD5Utils.encrypByMd5(employee.getPassword() + Constant.PRE_SALT + emp.getSalt() + Constant.END_SALT), emp.getPassword())) {
                // 密码对上了
                // 响应token 颁发令牌 生成uuid
                String uuid = UUID.randomUUID().toString();
                redisTemplate.opsForValue().set(uuid, emp.getUsername(), 30, TimeUnit.MINUTES);
                EmpDTO dto = new EmpDTO();
                dto.setUsername(employee.getUsername());
                dto.setToken(uuid);
                dto.setId(emp.getId());

                ArrayList<Map<String, Object>> list = new ArrayList<>();
                HashMap<String, Object> map = new HashMap<>();
                map.put( "path","/dashboard");
                map.put("name", "Dashboard");
                list.add(map);
                map = new HashMap<>();
                map.put( "path","/admin");
                map.put("name", "Admin");
                list.add(map);
                dto.setMenuList(list);
                return dto;
            } else {
                // 密码错误
                throw new RuntimeException("用户名或密码错误");
            }
        }
    }


    @Override
    public AjaxResult logout() {
        redisTemplate.delete("token");
        return AjaxResult.me();
    }



    @Override
    public void SaveEmp(Emp emp) {
        if(emp.getId()!=null){
            mapper.updateEmp(emp);
        }else{
            mapper.addEmp(emp);
        }
    }

    @Override
    public void DelEmp(List<Long> ids) {
        mapper.delEmp(ids);
    }

    @Override
    public PageInfo<Emp> findAllEmp(EmpQuery query) {
        PageHelper.startPage(query.getCurrentPage(),query.getPageSize());
        List<Emp> list = mapper.findAllEmp(query);
        Page<Emp> page = (Page<Emp>) list;
        return page.toPageInfo();
    }
}
